home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / uwsrc.arc / WINPROTO.C < prev    next >
C/C++ Source or Header  |  1989-04-29  |  5KB  |  257 lines

  1. /*
  2.  * This file contains the routines which implement the uw protocol.
  3.  * Code has been migrating here from winmain.c, but its not all here yet.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <osbind.h>
  8. #include <gemdefs.h>
  9. #include "wind.h"
  10. #include "uw.h"
  11. #include "windefs.h"
  12.  
  13. #define LIMIT    300
  14.  
  15. extern    struct    wi_str    w[];
  16. extern int outport;            /* ports used with uw */
  17. extern int inwind, outwind;        /* windows for input/output */
  18. extern int uw_runs;            /* is uw running ? */
  19. FNT    *curfont;            /* font in use */
  20.  
  21. /* proto_out sends length chars from str to the window defined by outport. */
  22. proto_out(outport, str, length)
  23. int outport, length;
  24. char *str;
  25.  
  26. {
  27. static int oldoutport = 1;
  28. int key, i;
  29. int outwind;
  30.  
  31. outwind = find_wind(outport);
  32.   if (w[outwind].w_local == TRUE)    /* local window? */
  33.   {
  34.     char *found, *index();
  35.     char strcr[3] = "\r\n";
  36.  
  37.     str[length] = '\0';
  38.     while (found = index(str, '\r'))
  39.     {
  40.       char line[500];
  41.       strncpy(line, str, found - str);
  42.       line[found-str] = '\0';
  43.       w_output(outwind, line);
  44.       w_output(outwind, strcr);
  45.       str = found + 1;
  46.     }
  47.     w_output(outwind, str);
  48.     return;
  49.   }
  50.   for (key = *str, i = 0; i < length; key = str[++i])
  51.   {
  52.     if (uw_runs)
  53.     {
  54.       if (outport != oldoutport)
  55.       {
  56.         xmitcmd(CB_FN_ISELW|outport);
  57.         oldoutport = outport;
  58.       }
  59.       if (key & 0x80) {
  60.         key &= 0x7f;
  61.     xmitcmd(CB_FN_META);
  62.       }
  63.       switch (key)
  64.       {
  65.       case XON:
  66.         xmitcmd(CB_FN_CTLCH|CB_CC_ON);
  67.         break;
  68.  
  69.       case XOFF:
  70.         xmitcmd(CB_FN_CTLCH|CB_CC_OFF);
  71.         break;
  72.  
  73.       case IAC:
  74.         xmitcmd(CB_FN_CTLCH|CB_CC_IAC);
  75.         break;
  76.  
  77.       default:
  78.         Bconout(1, key);
  79.         break;
  80.       }
  81.     } else
  82.       Bconout(1, key);    /* need mask with 0x7f? */
  83.   }
  84. }
  85.  
  86. /*
  87.  * proto_close closes the current window and notifies the remote.  The new
  88.  * current window is returned.
  89.  */
  90. int proto_close(curwind)
  91. int curwind;
  92. {
  93.   long dummy;
  94.  
  95.   if (uw_runs && (find_port(curwind) < MAX_WIND))
  96.   {
  97.     xmitcmd(CB_FN_KILLW|find_port(curwind));
  98.   }
  99.   w_close(curwind);
  100.   wind_get(0, WF_TOP, &curwind, &dummy, &dummy, &dummy);
  101.   return (curwind);
  102. /*  } else
  103.   {
  104.     w_close(curwind);
  105.     return (0);
  106.   } */
  107. }
  108.  
  109. /* proto_in receives characters from the serial port. */
  110. proto_in()
  111. #define NORMSTATE 0
  112. #define IACSTATE 1
  113. /* #define METASTATE 2  replaced with seenmeta flag. */
  114. #define INITSTATE 3
  115. {
  116.   register char    *ptr;
  117.   char    str[LIMIT+2];
  118.   register char    chr;
  119.   register int    cnt;
  120.   long dummy;
  121.   static int state;
  122.   static int seenmeta = 0;
  123.  
  124.       ptr = str;
  125.       cnt = 0;
  126.       while (Bconstat(1) && cnt++<LIMIT)
  127.       {
  128.     chr = Bconin(1)&0x7f;
  129.     switch (state)
  130.     {
  131.     case NORMSTATE:
  132.       if (!chr || chr=='\177') continue;
  133.       if (chr == IAC)
  134.       {
  135.         if (uw_runs)
  136.           state = IACSTATE;
  137.         else
  138.           state = INITSTATE;
  139.         continue;
  140.       }
  141.       if (seenmeta) {
  142.         seenmeta = 0;
  143.         *ptr++ = chr|0x80;
  144.       }
  145.       else *ptr++ = chr;
  146.       break;
  147.     case INITSTATE:
  148.       if (chr == CB_FN_MAINT | CB_MF_ENTRY){
  149.         xmitcmd(CB_FN_MAINT|CB_MF_ENTRY);
  150.         uw_runs = 1;
  151.         outport = 0;
  152.         outwind = 0;
  153.       }
  154.       else {
  155.         *ptr++ = IAC;
  156.         *ptr++ = chr;
  157.       }
  158.       state = NORMSTATE;
  159.       break;
  160.     case IACSTATE:
  161.       state = NORMSTATE;
  162.       if (chr&CB_DIR_MTOH) continue;
  163.       switch (chr&CB_FN)
  164.       {
  165.       case CB_FN_NEWW:
  166.         outport = w_open(chr&CB_WINDOW, "Terminal", curfont->def_win_x,
  167.           curfont->def_win_y);
  168.         outwind = find_wind(outport);
  169.         xmitcmd(CB_FN_ISELW|outport);
  170.         break;
  171.  
  172.       case CB_FN_KILLW:
  173.         w_close(find_wind(chr&CB_WINDOW));
  174.         wind_get(0, WF_TOP, &outwind, &dummy, &dummy, &dummy);
  175.         outport = find_port(outwind);
  176.         break;
  177.  
  178.       case CB_FN_OSELW:
  179.         if (inwind != find_wind(chr&CB_WINDOW))
  180.         {
  181.           *ptr = '\0';
  182.           if (ptr != str)
  183.           {
  184.             if (w[inwind].kerm_act)
  185.           rpack(" ", NULL, str);
  186.         else
  187.           w_output(inwind, str);
  188.           }
  189.           ptr = str;
  190.         }
  191.         inwind = find_wind(chr&CB_WINDOW);
  192.         break;
  193.  
  194.       case CB_FN_META:
  195.         seenmeta = 1;
  196.         break;
  197.  
  198.       case CB_FN_CTLCH:
  199.         switch (chr&CB_CC)
  200.         {
  201.         case CB_CC_IAC:
  202.           if (seenmeta) {
  203.             seenmeta = 0;
  204.             *ptr++ = IAC|0x80;
  205.           }
  206.           else *ptr++ = IAC;
  207.           break;
  208.  
  209.         case CB_CC_ON:
  210.           if (seenmeta) {
  211.             seenmeta = 0;
  212.             *ptr++ = XON|0x80;
  213.           }
  214.           else *ptr++ = XON;
  215.           break;
  216.  
  217.         case CB_CC_OFF:
  218.           if (seenmeta) {
  219.             seenmeta = 0;
  220.             *ptr++ = XOFF|0x80;
  221.           }
  222.           else *ptr++ = XOFF;
  223.  
  224.           break;
  225.         }
  226.         break;
  227.  
  228.       case CB_FN_MAINT:
  229.         switch (chr&CB_MF)
  230.         {
  231.         case CB_MF_ENTRY:
  232.           xmitcmd(CB_FN_MAINT|CB_MF_ENTRY);
  233.           uw_runs = 1;
  234.           outport = 0;
  235.           outwind = 0;
  236.           break;
  237.  
  238.         case CB_MF_EXIT:
  239.           uw_runs = 0;
  240.           return (-1);
  241.         }
  242.         break;
  243.       }
  244.       break;
  245.     }
  246.       }
  247.       *ptr = '\0';
  248.       if (ptr!=str)
  249.       {
  250.         if (w[inwind].kerm_act)
  251.       rpack(" ", NULL, str);
  252.     else
  253.       w_output(inwind, str);
  254.       }
  255.       return (0);
  256. }
  257.